<?php

namespace app\common\model;

use think\Model;
use think\Db;

class UserModel extends Model
{
    protected $pk = 'id';
    protected $table = 'basic_user';
    protected $autoWriteTimestamp = true;

    public function getUserList($params = [])
    {
        $curPage = 1;
        $pageSize = 15;
        $where['status'] = ['neq', 111];
        if (!empty($params)) {
            checkString($params['nickname']) && $where['nickname'] = ['like', "%" . $params['nickname'] . "%"];
        }

        $totalRow = self::where($where)->count();
        $data = self::where($where)->select();
        $data = empty($data) ? [] : collection($data)->toArray();

        return [
            'pageSize' => $pageSize,
            'totalRow' => $totalRow,
            'curPage' => $curPage,
            'data' => $data
        ];
    }

    //获取单个用户
    public function getUserById($id)
    {
        $user = Db::table($this->table)->find($id);
        return empty($user) ? [] : collection($user)->toArray();
    }

    public function getUserByToken($token){
        $user = Db::table($this->table)->field("id,nickname,figureurl")->where(['token'=>$token])->find();
        return $user;
    }

    //获取单个用户
    public function getUserByOpenId($openId)
    {
        $user = Db::table($this->table)->where(['openid' => $openId])->find();
        return empty($user) ? [] : collection($user)->toArray();
    }

    //批量改变状态
    public function changeStatus($ids, $status)
    {
        return self::where("id", "in", $ids)->update([
            'status' => $status,
            'update_time' => time()
        ]);
    }
}
